package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.GridAdapter;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CommentActivity extends AppCompatActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.content_et)
    EditText contentEt;
    @BindView(R.id.send_btn)
    Button sendBtn;
    @BindView(R.id.rating_bar1)
    SimpleRatingBar ratingBar;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    //    @BindView(R.id.recycler)
//    LRecyclerView recycler;
    private String thumbUrl;
    private String userId;
    private String videoId;
    private GridAdapter gridAdapter;
    private int comment_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        ButterKnife.bind(this);
        thumbUrl = getIntent().getStringExtra("url");
        videoId = getIntent().getStringExtra("id");
        comment_type = getIntent().getIntExtra("comment_type", -1);
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
       /* String res=StringUtil.test1("ffsdf%F0%9F%98%81%F0%9F%98%82哈哈");
        contentEt.setText(res);*/
        initView();
//        initImg();
    }

    private void initImg() {
//        List<String> lists=new ArrayList<>();
//        if(!thumbUrl.contains(",")){
//            Glide.with(getApplicationContext()).load(thumbUrl).error(R.mipmap.error_small).into(image);
//        }else{
//            String[] urls=thumbUrl.split(",");
//            lists.addAll(Arrays.asList(urls)) ;
//        }
//        recycler.setLayoutManager(new GridLayoutManager(this,2));
//        gridAdapter=new GridAdapter(this);
//        gridAdapter.setDataList(lists);
//        LRecyclerViewAdapter lRecyclerViewAdapter = new LRecyclerViewAdapter(gridAdapter);
//        lRecyclerViewAdapter.removeFooterView();
//        lRecyclerViewAdapter.removeHeaderView();
//        recycler.setAdapter(lRecyclerViewAdapter);
//        recycler.setLoadMoreEnabled(false);
//        recycler.setPullRefreshEnabled(false);
//        DividerDecoration divider = new DividerDecoration.Builder(this)
//                .setHeight(R.dimen.divider_height)
//                .setPadding(R.dimen.divider_padding)
//                .setColorResource(R.color.line_gray)
//                .build();
//        recycler.addItemDecoration(divider);
//        recycler.refresh();
    }

    private void initView() {
        Glide.with(getApplicationContext()).load(thumbUrl).error(R.mipmap.error_small).into(image);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleRightOneTv.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("发表评论");
        titleRightOneTv.setText("完成");
        ratingBar.setOnRatingBarChangeListener(new SimpleRatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(SimpleRatingBar simpleRatingBar, float rating, boolean fromUser) {
                doGrade(Math.round(rating));
            }
        });

    }

    private void doGrade(int rating) {
        DialogUtils.showProgressDlg(this, "");
        OkGo.<String>post(Constant.GRADE)
                .tag(this)
                .params("video_id", videoId)
                .params("user_id", userId)
                .params("score", rating)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        DealResult dealResult = new DealResult();
//                        List<StatusBean> beans = dealResult.deal(CommentActivity.this, response, new TypeToken<BaseBean<StatusBean>>() {
//                        }.getType());
//                        ToastUtils.toastForShort(CommentActivity.this, "评分成功！");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(CommentActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    @OnClick({R.id.title_left_one_btn, R.id.send_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                CommentActivity.this.finish();
                break;
            case R.id.send_btn:
                String content = contentEt.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    ToastUtils.toastForShort(CommentActivity.this, "内容不能为空！");
                    return;
                }
                publishComment(content);

                break;
        }
    }

    private void publishComment(String content) {
        DialogUtils.showProgressDlg(this, "");
//      comment_type  0、商品 1、文章2、视频
        OkGo.<String>post(Constant.CONFIRM_COMMENT)
                .tag(this)
                .params("user_id", userId)
                .params("content", content)
                .params("comment_type", comment_type)
                .params("id_value", videoId)
                .params("comment_rank", 0)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        String res = response.body().toString();
//                        if (TextUtils.isEmpty(res)) {
//                            ToastUtils.toastForShort(CommentActivity.this, "评论失败！");
//                        } else {
//                            BaseBean bean = new Gson().fromJson(res, BaseBean.class);
//                            if (bean.getStatus() == Constant.SUCESS) {
//                                CommentActivity.this.finish();
//                            }
//                            ToastUtils.toastForShort(CommentActivity.this, bean.getMsg());
//                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(CommentActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
}
